package com.example.android.chartapplication;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

public class ChartRadar extends View {
    public ChartRadar(Context context) {
        super(context);
    }

    public ChartRadar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        setDatas(new float[] {20, 30, 50, 60, 80} );
    }

    public ChartRadar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private static final int DATA_NUM = 5;
    private static float startAngle;
    private static final String[] TEXTS = new String[] {"Data1","Data2","Data3","Data4","Data5"};
    float[] datas_max = new float[] {100, 100, 100, 100, 100};
    float[] datas;
    Paint paint = new Paint();

    public void setDatas_max(float[] data) {
        datas = data;
    }

    public void setDatas(float[] data) {
        datas = data;
    }

    Paint contentPaint = new Paint();
    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
        canvas.save();
        contentPaint.setAntiAlias(true);
        contentPaint.setColor(Color.CYAN);
        contentPaint.setAlpha(255);

        Path path = new Path();
        for (int i = 0; i < DATA_NUM; i++) {
            float X = makeX(startAngle + 360 / DATA_NUM * i, datas[i] / datas_max[i] * 130);
            float Y = makeY(startAngle + 360 / DATA_NUM * i, datas[i] / datas_max[i] * 130);
            canvas.drawCircle(X, Y, 3, contentPaint);
            canvas.drawText(datas[i] + "", X, Y, paint);
            if (i == 0) path.moveTo(X, Y);
            else path.lineTo(X, Y);
        }
        path.close();
        contentPaint.setAlpha(150);
        canvas.drawPath(path, contentPaint);

        canvas.restore();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.translate(150, 150);
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.STROKE);
        startAngle = -360 * DATA_NUM / 100;

        for (int i = 1; i < 5; i++) {
            canvas.drawPath(makePath(DATA_NUM, 130f / 4 * i), paint);
        }
        for (int i = 0; i < DATA_NUM; i++) {

            float X = makeX(startAngle + 360 / DATA_NUM * i, 130);
            float Y = makeY(startAngle + 360 / DATA_NUM * i, 130);
            float textX = makeX(startAngle + 360 / DATA_NUM * i, 140);
            float textY = makeY(startAngle + 360 / DATA_NUM * i, 140);
            canvas.drawLine(X, Y, 0, 0, paint);
            canvas.drawText(TEXTS[i], textX, textY, paint);
        }
    }

    private static Path makePath(int num, float r) {
        Path path = new Path();
        float x = makeX(startAngle, r);
        float y = makeY(startAngle, r);
        path.moveTo(x, y);
        for (int i = 1; i < num; i++) {
            float nextX = makeX(startAngle + 360 / DATA_NUM * i, r);
            float nextY = makeY(startAngle + 360 / DATA_NUM * i, r);
            path.lineTo(nextX, nextY);
        }
        path.close();
        return path;
    }

    public static float makeX(float angle, float r) {
        return (float) Math.cos(Math.toRadians(angle)) * r;
    }

    public static float makeY(float angle, float r) {
        return (float) Math.sin(Math.toRadians(angle)) * r;
    }
}
